package com.zhq.utils.gilde;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapResource;
import com.zhq.apputil.widget.config.GildeImageConfig;

import java.security.MessageDigest;

import androidx.annotation.NonNull;

/**
 * @author ZHQ
 * @create 2019/11/21
 * @Describe 绘制圆柱形的图片
 */
public class CornerTransform implements Transformation<Bitmap> {
    private BitmapPool mBitmapPool;

    private float radius;

    private boolean exceptLeftTop, exceptRightTop, exceptLeftBottom, exceptRightBotoom;

    private float mBorderWidth;

    private Paint mBorderPaint;


    public CornerTransform(Context context, GildeImageConfig config) {
        this.mBitmapPool = Glide.get(context).getBitmapPool();
        this.radius = config.getRadius();
        this.mBorderWidth = config.getBorderWidth();
        this.exceptLeftTop = config.isLeftTop();
        this.exceptRightTop = config.isRightTop();
        this.exceptLeftBottom = config.isLeftBottom();
        this.exceptRightBotoom = config.isRightBottom();
        mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBorderPaint.setDither(true);
        mBorderPaint.setAntiAlias(true);
        mBorderPaint.setColor(config.getBorderColor());
        mBorderPaint.setStyle(Paint.Style.STROKE);
        mBorderPaint.setStrokeWidth(mBorderWidth);

    }

    @NonNull
    @Override
    public Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> resource, int outWidth, int outHeight) {
        Bitmap source = resource.get();
        int finalWidth, finalHeight;
        float ratio; //输出目标的宽高或高宽比例
        if (outWidth > outHeight) { //输出宽度>输出高度,求高宽比
            ratio = (float) outHeight / (float) outWidth;
            finalWidth = source.getWidth();
            finalHeight = (int) ((float) source.getWidth() * ratio); //固定原图宽度,求最终高度
            if (finalHeight > source.getHeight()) { //求出的最终高度>原图高度,求宽高比
                ratio = (float) outWidth / (float) outHeight;
                finalHeight = source.getHeight();
                finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
            }
        } else if (outWidth < outHeight) { //输出宽度 < 输出高度,求宽高比
            ratio = (float) outWidth / (float) outHeight;
            finalHeight = source.getHeight();
            finalWidth = (int) ((float) source.getHeight() * ratio);//固定原图高度,求最终宽度
            if (finalWidth > source.getWidth()) { //求出的最终宽度 > 原图宽度,求高宽比
                ratio = (float) outHeight / (float) outWidth;
                finalWidth = source.getWidth();
                finalHeight = (int) ((float) source.getWidth() * ratio);
            }
        } else { //输出宽度=输出高度
            finalHeight = source.getHeight();
            finalWidth = finalHeight;
        }

        float widthScale = outWidth * 1f / finalWidth;
        float heightScale = outHeight * 1f / finalHeight;

        Matrix matrix = new Matrix();
        matrix.setTranslate(widthScale, heightScale);
        //修正圆角
        this.radius *= (float) finalHeight / (float) outHeight;
        Bitmap outBitmap = this.mBitmapPool.get(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
        if (outBitmap == null) {
            outBitmap = Bitmap.createBitmap(finalWidth, finalHeight, Bitmap.Config.ARGB_8888);
        }
        Canvas canvas = new Canvas(outBitmap);
        //关联画笔绘制的原图bitmap
        BitmapShader shader = new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        shader.setLocalMatrix(matrix);
//
//        //计算中心位置,进行偏移
//        int width = (source.getWidth() - finalWidth) / 2;
//        int height = (source.getHeight() - finalHeight) / 2;
//        if (width != 0 || height != 0) {
//            Matrix matrix = new Matrix();
//            matrix.setTranslate((float) (-width), (float) (-height));
//            shader.setLocalMatrix(matrix);
//        }
        Paint paint = new Paint();
        paint.setShader(shader);
        paint.setAntiAlias(true);
        if (mBorderWidth > 0) {
            //创建矩形区域并且预留出border
            RectF rect = new RectF(mBorderWidth, mBorderWidth, finalWidth - mBorderWidth, finalHeight - mBorderWidth);
            canvas.drawRoundRect(rect, radius, radius, paint); //先绘制圆角矩形
            canvas.drawRoundRect(rect, radius, radius, mBorderPaint); //绘制boarder
        } else {
            RectF rectF = new RectF(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint); //先绘制圆角矩形
            if (!exceptLeftTop) { //左上角不为圆角
                canvas.drawRect(0, 0, radius, radius, paint);
            }
            if (!exceptRightTop) {//右上角不为圆角
                canvas.drawRect(canvas.getWidth() - radius, 0, canvas.getWidth(), radius, paint);
            }
            if (!exceptLeftBottom) {//左下角不为圆角
                canvas.drawRect(0, canvas.getHeight() - radius, radius, canvas.getHeight(), paint);
            }

            if (!exceptRightBotoom) {//右下角不为圆角
                canvas.drawRect(canvas.getWidth() - radius, canvas.getHeight() - radius, canvas.getWidth(), canvas.getHeight(), paint);
            }
        }
        return BitmapResource.obtain(outBitmap, this.mBitmapPool);
    }


    @Override
    public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {

    }
}
